<?php
/**
 * Created By PhpStorm
 * Author: patrick wu
 * Date: 2021/1/7
 * Time: 9:42
 */
namespace App\Http\Controllers\Api;

use App\Service\Api\IntegralService;
use Illuminate\Support\Facades\Validator;
use Illuminate\Http\Request;
use App\Constant\ErrorConstant;

class IntegralController extends BaseController
{
    protected $service;

    public function __construct(Request $request, IntegralService $service)
    {
        parent::__construct($request);
        $this->service  =   $service;
    }

    /**
     * 查看用户个人积分
     * @return \Illuminate\Http\JsonResponse
     */
    public function personalIntegral()
    {
        [$flag, $result]    =   $this->service->integralPersonal();
        if ($flag) {
            return $this->successResponse($result);
        } else {
            return $this->errorResponse(ErrorConstant::API_SCORE_PERSONAL_ERROR, $result);
        }
    }

    /**
     * 查看用户积分记录
     * @return \Illuminate\Http\JsonResponse
     */
    public function incomeIntegral()
    {
        $page   =   (int) $this->request->input('page', 1);

        [$flag, $result]    =   $this->service->integralIncome($page);
        if ($flag) {
            return $this->successResponse($result);
        } else {
            return $this->errorResponse(ErrorConstant::API_SCORE_INCOME_ERROR, $result);
        }
    }

    /**
     * 查看积分商品
     * @return \Illuminate\Http\JsonResponse
     */
    public function goodsIntegral()
    {
        $validator  =   Validator::make($this->request->post(), [
            'is_vip'    =>  'required|integer|in:0,1'
        ], [
            'is_vip.required'   =>  '是否会员不能为空',
            'is_vip.integer'    =>  '是否会员必须为整型',
            'is_vip.in'         =>  '是否会员选择错误'
        ]);
        $page   =   (int) $this->request->input('page', 1);
        $is_vip =   (int) $this->request->post('is_vip');

        [$flag, $result]    =   $this->service->integralGoods($is_vip, $page);
        if ($flag) {
            return $this->successResponse($result);
        } else {
            return $this->errorResponse(ErrorConstant::API_SCORE_INCOME_ERROR, $result);
        }
    }

    /**
     * 查看积分兑换记录
     * @return \Illuminate\Http\JsonResponse
     */
    public function exchangeIntegral()
    {
        $page   =   (int) $this->request->input('page', 1);

        [$flag, $result]    =   $this->service->integralExchange($page);
        if ($flag) {
            return $this->successResponse($result);
        } else {
            return $this->errorResponse(ErrorConstant::API_SCORE_EXCHANGE_ERROR, $result);
        }
    }
}
